package com.xique.park.service.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.web.domain.PageResult;
import com.xique.park.bean.domain.ParkIn;
import com.xique.park.bean.domain.ParkOutV1;
import com.xique.park.bean.domain.ParkPlatemodifyRecord;
import com.xique.park.bean.vo.web.platemodifyRecord.ParkPlatemodifyRecordVo;
import com.xique.park.controller.client.web.bean.search.ParkOutSearch;
import com.xique.park.mapper.ParkPlatemodifyRecordMapper;
import com.xique.park.service.service.IParkInService;
import com.xique.park.service.service.IParkOutService;
import com.xique.park.service.service.IParkPlatemodifyRecordServce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 车牌修改记录service
 *
 * @author xique
 */
@Service
public class ParkPlatemodifyRecordServiceImpl extends ServiceImpl<ParkPlatemodifyRecordMapper, ParkPlatemodifyRecord> implements IParkPlatemodifyRecordServce {

    @Autowired
    private IParkInService parkInService;

    @Autowired
    private IParkOutService parkOutService;

    @Override
    public PageResult getParkPlatemodifyRecordList(Long parkId, Long laneId, String startTime, String endTime,
                                                   String modifiedPlate, Integer pageNum, Integer pageSize) {
        Page<ParkPlatemodifyRecordVo> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        IPage<ParkPlatemodifyRecordVo> list = baseMapper.getParkPlatemodifyRecord(parkId, laneId, startTime, endTime, modifiedPlate, page);
        List<ParkPlatemodifyRecordVo> parkPlatemodifyRecordVoList = list.getRecords();
        // 处理outID BY WLD 2022-4/27
        handelOutId(parkPlatemodifyRecordVoList);
        long totalPages = list.getPages();
        long totalSize = list.getTotal();
        return PageResult.builder()
                .pageNum(pageNum)
                .pageSize(pageSize)
                .totalPages(totalPages)
                .totalSize(totalSize)
                .list(parkPlatemodifyRecordVoList)
                .build();
    }

    private void handelOutId(List<ParkPlatemodifyRecordVo> parkPlatemodifyRecordVoList) {
        if (CollectionUtil.isNotEmpty(parkPlatemodifyRecordVoList)) {
            parkPlatemodifyRecordVoList.forEach(parkPlatemodifyRecordVo -> {
                Long inId = parkPlatemodifyRecordVo.getInId();
                if (null != inId) {
                    // 查找入场信息，获取停车流水号
                    ParkIn parkIn = parkInService.selectParkInById(inId);
                    if (null != parkIn) {
                        // 根据停车流水号获取出场信息
                        String parkingSerial = parkIn.getParkingSerial();
                        ParkOutSearch search = new ParkOutSearch();
                        search.setParkId(SecurityUtils.getParkId());
                        search.setParkingSerial(parkingSerial);
                        ParkOutV1 parkOut = parkOutService.selectParkOut(search);
                        if (null != parkOut) {
                            parkPlatemodifyRecordVo.setOutId(parkOut.getId());
                        }
                    }
                }
            });
        }
    }

}
